package com.zhht.htzx.ui;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.ToastUtils;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;

public class WithdrawalActivity extends BaseActivity {


    private WithdrawalStateHolder mStates;
    private AccountRequester mAccountRequester;

    private double amountWithdrawal = 0.0;

    @Override
    protected void initViewModel() {

        mStates = getActivityScopeViewModel(WithdrawalStateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_withdrawal_apply, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.titleBar.set("申请提现");
        mStates.amountWithdrawal.set(getIntent().getDoubleExtra("amountWithdrawal", 0.0));
        mStates.amountWithdrawalSrc.set("当前可提现金额" + APIs.df.format(mStates.amountWithdrawal.get() / 100) + "元");
        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {

                getController().dismissLoading();
                if (stringResponseStatus.getResponseCode() == 0) {
                    finish();

                    ToastUtils.showShortToast(stringResponseStatus.getMsg());
                }
            }
        });
    }

    public static class WithdrawalStateHolder extends StateHolder {
        public final State<Double> amountWithdrawal = new State<>(0.0);//可提现金额
        public final State<String> amountWithdrawalSrc = new State<>("");//可提现金额
        public final State<String> withdrawalPrice = new State<>("");
        public final State<String> withdrawalName = new State<>("");
        public final State<String> withdrawalCard = new State<>("");
        public final State<String> withdrawalBlank = new State<>("");

    }


    public class ClickProxy {

        public void amountWithdrawal() {
            mStates.withdrawalPrice.set(APIs.df.format(mStates.amountWithdrawal.get() / 100));
        }

        public void openWithdrawalRecord() {
            startActivity(new Intent().setClass(WithdrawalActivity.this, WithdrawalRecordActivity.class));
        }

        public void submit() {

            try {

                if (Double.parseDouble(mStates.withdrawalPrice.get()) > mStates.amountWithdrawal.get()) {
                    ToastUtils.showShortToast("提现金额不能大于可提现金额");
                    return;
                } else if (Double.parseDouble(mStates.withdrawalPrice.get()) < 100) {
                    ToastUtils.showShortToast("提现金额不能小于100元");
                    return;
                }
            } catch (NumberFormatException e) {
                e.getMessage();
            }

            getController().displayLoading();
            JsonObject params = new JsonObject();
            params.addProperty("withdrawalAmount", mStates.withdrawalPrice.get());
            params.addProperty("withdrawName", mStates.withdrawalName.get());
            params.addProperty("openingBank", mStates.withdrawalBlank.get());
            params.addProperty("bankCardNumber", mStates.withdrawalCard.get());
            mAccountRequester.withdrawalApply(params);

        }
    }
}
